package org.apache.james.mime4j.message;

import java.io.IOException;
import java.io.InputStream;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.decoder.Base64InputStream;
import org.apache.james.mime4j.decoder.QuotedPrintableInputStream;
import org.apache.james.mime4j.descriptor.BodyDescriptor;
import org.apache.james.mime4j.field.Field;
import org.apache.james.mime4j.parser.AbstractContentHandler;
import org.apache.james.mime4j.util.MimeUtil;

public abstract class SimpleContentHandler extends AbstractContentHandler
{
  private Header currHeader;

  public final void body(BodyDescriptor paramBodyDescriptor, InputStream paramInputStream)
    throws IOException
  {
    if (MimeUtil.isBase64Encoding(paramBodyDescriptor.getTransferEncoding()))
      bodyDecoded(paramBodyDescriptor, new Base64InputStream(paramInputStream));
    while (true)
    {
      while (true)
      {
        return;
        if (!(MimeUtil.isQuotedPrintableEncoded(paramBodyDescriptor.getTransferEncoding())))
          break;
        bodyDecoded(paramBodyDescriptor, new QuotedPrintableInputStream(paramInputStream));
      }
      bodyDecoded(paramBodyDescriptor, paramInputStream);
    }
  }

  public abstract void bodyDecoded(BodyDescriptor paramBodyDescriptor, InputStream paramInputStream)
    throws IOException;

  public final void endHeader()
  {
    Header localHeader = this.currHeader;
    this.currHeader = null;
    headers(localHeader);
  }

  public final void field(String paramString)
    throws MimeException
  {
    this.currHeader.addField(Field.parse(paramString));
  }

  public abstract void headers(Header paramHeader);

  public final void startHeader()
  {
    this.currHeader = new Header();
  }
}